package uit.cinemainfo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter {
	private static final String DB_NAME = "efilm";
	private static final String DB_NAME_VI = "vfilm";
	private static final int DB_VERSION = 1;
	
	private final Context context;
	private DatabaseHelper DBHelper;
	private SQLiteDatabase  db;
	private String lang;
	
	public DBAdapter(Context ctx){
		this.context = ctx;
		this.lang = this.context.getResources().getConfiguration().locale.getLanguage();
		if(lang.equals("vi")){
			DBHelper = new DatabaseHelper(context, DB_NAME_VI);
		}else{
			DBHelper = new DatabaseHelper(context, DB_NAME);
		}
	}
	
	private static class DatabaseHelper extends SQLiteOpenHelper{
		DatabaseHelper(Context context, String databaseName){
			super(context, databaseName, null, DB_VERSION);
		}
		
		@Override
		public void onCreate(SQLiteDatabase db){
			
		}
		
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
			Log.w("DBAdapter", "Upgrading database from version " + oldVersion + " to "
					+ newVersion);
			
		}
	}
	
	//---Open the database---
	public DBAdapter open() throws SQLException {
		db = DBHelper.getWritableDatabase();
		return this;
	}
	
	//---close the database---
	public void close(){
		DBHelper.close();
	}
	
	public Cursor getAllShowing(){
		return db.query("film", new String[] {"trailer", "start", "nofilm", "namefilm", "description", "credit", "score", "image", "duration", "favorite", "kind", "numvote"}, "start <= date('now', '+7 day')", null, null, null, null);
	}
	
	public Cursor getAllComing(){
		return db.query("film", new String[] {"trailer", "start", "nofilm", "namefilm", "description", "credit", "score", "image", "duration", "favorite", "kind", "numvote"}, "start > date('now', '+7 day')", null, null, null, null);
	}
	
	public Cursor getFilm(int No){
		Cursor rCursor = db.query(true, "film", new String[] {"trailer", "start", "nofilm", "namefilm", "description", "credit", "score", "image", "duration", "favorite", "kind", "numvote"}, "nofilm = " + No, null, null, null, null, null);
		if(rCursor != null){
			rCursor.moveToFirst();
		}
		return rCursor;
	}
	
	public boolean updateVoteFilm(int No, float score, int numvote){
		ContentValues args = new ContentValues();
		args.put("score", score);
		args.put("numvote", numvote);
		return db.update("film", args, "nofilm = " + No, null) > 0;
	}
	
	public boolean updateFilm(Film film){
		ContentValues args = new ContentValues();
		args.put("trailer", film.trailer);
		args.put("score", film.score);
		args.put("numvote", film.numvote);
		args.put("start", film.start);
		args.put("namefilm", film.namefilm);
		args.put("description", film.description);
		args.put("credit", film.credit);
		args.put("image", film.image);
		args.put("duration", film.duration);
		args.put("kind", film.kind);
		args.put("favorite", film.favorite);
		return db.update("film", args, "nofilm = " + film.nofilm, null) > 0;
	}
	
	public boolean updateTheater(Theater theater){
		ContentValues args = new ContentValues();
		args.put("imgtheater", theater.imgtheater);
		args.put("namecine", theater.namecine);
		args.put("addcine", theater.addcine);
		args.put("telcine", theater.telcine);
		args.put("lat", theater.lat);
		args.put("lng", theater.lng);
		return db.update("cinema", args, "nocine = " + theater.nocine, null) > 0;
	}
	
	public boolean updateSchedule(Schedule schedule){
		ContentValues args = new ContentValues();
		args.put("nofilm", schedule.nofilm);
		args.put("nocine", schedule.nocine);
		args.put("showtime", schedule.showtime);
		args.put("alarm", schedule.alarm);
		return db.update("schedule", args, "nosche = " + schedule.nosche, null) > 0;
	}
	
	public Cursor getAllTheater(){
		return db.query("cinema", new String[] {"imgtheater", "nocine", "namecine", "addcine", "telcine", "lat", "lng"}, null, null, null, null, "nocine ASC");
	}
	
	public Cursor getScheByFilmCine(int nofilm, int nocine){
		return db.query("schedule", new String[] {"nosche", "nofilm", "nocine", "showtime"}, "nofilm = " + nofilm + " and nocine = " + nocine + " and showtime > date('now') and showtime < date('now', '+7 day')", null, null, null, null);
	}
	
	public boolean insertFilm(Film film){
		ContentValues args = new ContentValues();
		args.put("trailer", film.trailer);
		args.put("nofilm", film.nofilm);
		args.put("score", film.score);
		args.put("numvote", film.numvote);
		args.put("start", film.start);
		args.put("namefilm", film.namefilm);
		args.put("description", film.description);
		args.put("credit", film.credit);
		args.put("image", film.image);
		args.put("duration", film.duration);
		args.put("kind", film.kind);
		args.put("favorite", film.favorite);
		long status = db.insert("film", null, args);
		if(status == -1) {return false;}
		return true;
	}
	
	public boolean insertTheater(Theater theater){
		ContentValues args = new ContentValues();
		args.put("imgtheater", theater.imgtheater);
		args.put("nocine", theater.nocine);
		args.put("namecine", theater.namecine);
		args.put("addcine", theater.addcine);
		args.put("telcine", theater.telcine);
		args.put("lat", theater.lat);
		args.put("lng", theater.lng);

		long status = db.insert("cinema", null, args);
		if(status == -1) {return false;}
		return true;
	}
	
	public boolean insertSchedule(Schedule schedule){
		ContentValues args = new ContentValues();
		args.put("nosche", schedule.nosche);
		args.put("nofilm", schedule.nofilm);
		args.put("nocine", schedule.nocine);
		args.put("showtime", schedule.showtime);
		args.put("alarm", schedule.alarm);

		long status = db.insert("schedule", null, args);
		if(status == -1) {return false;}
		return true;
	}
	
	public boolean updateFilmTable(Film film){
		Cursor rCursor = db.query(true, "film", new String[] {"trailer", "start", "nofilm", "namefilm", "description", "credit", "score", "image", "duration", "favorite", "kind", "numvote"}, "nofilm = " + String.valueOf(film.nofilm), null, null, null, null, null);
		if((rCursor.moveToFirst()) && rCursor.getCount() > 0){
			return updateFilm(film);
		}else{
			return insertFilm(film);
		}
	}
	
	public boolean updateCinemaTable(Theater theater){
		Cursor rCursor = db.query(true, "cinema", new String[] {"imgtheater", "nocine", "namecine", "addcine", "telcine", "lat", "lng"}, "nocine = " + String.valueOf(theater.nocine), null, null, null, null, null);
		if((rCursor.moveToFirst()) && rCursor.getCount() > 0){
			return updateTheater(theater);
		}else{
			return insertTheater(theater);
		}
	}
	
	public boolean updateScheduleTable(Schedule schedule){
		Cursor rCursor = db.query(true, "schedule", new String[] {"nosche", "nofilm", "nocine", "showtime", "alarm"}, "nosche = " + String.valueOf(schedule.nosche), null, null, null, null, null);
		if((rCursor.moveToFirst()) && rCursor.getCount() > 0){
			return updateSchedule(schedule);
		}else{
			return insertSchedule(schedule);
		}
	}
}
